package com.ibilibili.crawl;


import org.json.JSONArray;
import org.json.JSONObject;
import com.ibilibili.utils.MysqlClient;
import com.ibilibili.utils.Consts;

public class JSONDeal {

    // json字符串
    private String jsonMessage = "";

    public JSONDeal() {
    }

    public JSONDeal(String jsonMessage) {
        this.jsonMessage = jsonMessage;
    }

    public void parse() {
        this.parse(this.jsonMessage);
    }

    public void parse(String jsonMessage) {
        try {
            MysqlClient mysql = new MysqlClient("jdbc:mysql://localhost:3306/recommendDB?useSSL=true&useUnicode=true&characterEncoding=gb2312", "root", "xxx");
//            MysqlClient mysql = new MysqlClient("jdbc:mysql://192.168.1.103:3306/recommendDB?useSSL=true&useUnicode=true&characterEncoding=gb2312", "root", "xxx");
            JSONObject myJsonObject = new JSONObject(jsonMessage);
            String code = myJsonObject.getString("code");
            // return if no data
            if (! "0".equals(code)) {
                return;
            }
            JSONObject dataJsonObject = myJsonObject.getJSONObject("data");
            JSONArray archivesJsonArray = dataJsonObject.getJSONArray("archives");
            //            System.out.println("archivesJsonObject: " + archivesJsonArray);
            for (int i = 0; i < archivesJsonArray.length(); i++) {
                // 获取每一个JSONObject对象
                JSONObject jo = archivesJsonArray.getJSONObject(i);
                JSONObject statJo = jo.getJSONObject("stat");

                String aid = jo.getString(Consts.AID);
                String tid = jo.getString(Consts.TID);
                String tname = jo.getString(Consts.TNAME);
                String copyright = jo.getString(Consts.COPYRIGHT);
                String pic = jo.getString(Consts.PIC);
                String title = jo.getString(Consts.TITLE);
                String attribute = jo.getString(Consts.ATTRIBUTE);
                String duration = jo.getString(Consts.DURATION);
                String play = jo.getString(Consts.PLAY);
                String favorites = jo.getString(Consts.FAVORITES);
                String video_review = jo.getString(Consts.VIDEO_REVIEW);
                String create = jo.getString(Consts.CREATE);
                //  String description = jo.getString(Consts.DESCRIPTION);
                String mid = jo.getString(Consts.MID);
                String author = jo.getString(Consts.AUTHOR);
                String face = jo.getString(Consts.FACE);
                String view = statJo.getString(Consts.VIEW);
                String danmaku = statJo.getString(Consts.DANMAKU);
                String reply = statJo.getString(Consts.REPLY);
                String favorite = statJo.getString(Consts.FAVORITE);
                String coin = statJo.getString(Consts.COIN);
                String share = statJo.getString(Consts.SHARE);
                String now_rank = statJo.getString(Consts.NOW_RANK);
                String his_rank = statJo.getString(Consts.HIS_RANK);

//
//                System.out.println("aid: " + aid);
//                System.out.println("tid: " + tid);
//                System.out.println("tname: " + tname);
//                System.out.println("copyright: " + copyright);
//                System.out.println("pic: " + pic);
//                System.out.println("title: " + title);
//                System.out.println("attribute: " + attribute);
//                System.out.println("duration: " + duration);
//                System.out.println("view: " + view);
//                System.out.println("danmaku: " + danmaku);
//                System.out.println("reply: " + reply);
//                System.out.println("favorite: " + favorite);
//                System.out.println("coin: " + coin);
//                System.out.println("share: " + share);
//                System.out.println("now_rank: " + now_rank);
//                System.out.println("his_rank: " + his_rank);
//                System.out.println("play: " + play);
//                System.out.println("favorites: " + favorites);
//                System.out.println("video_review: " + video_review);
//                System.out.println("create: " + create);
//                System.out.println("description: " + description);
//                System.out.println("mid: " + mid);
//                System.out.println("author: " + author);
//                System.out.println("face: " + face);

                String jsonArr [] = {aid, tid, tname, copyright, pic, title, attribute, duration, view, danmaku, reply,
                        favorite, coin, share, now_rank, his_rank, play, favorites, video_review, create,
                        mid, author, face};
                mysql.write(jsonArr);

            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
